import { useTranslation } from 'react-i18next';

export interface SEOConfig {
  title: string;
  description: string;
  keywords?: string;
  image?: string;
  url?: string;
  type?: 'website' | 'article';
  locale?: string;
  siteName?: string;
}

export const getBaseURL = () => {
  if (typeof window !== 'undefined') {
    return window.location.origin;
  }
  return 'https://www.mamepay.com';
};

export const getLanguageConfig = (language: string) => {
  const configs = {
    ja: {
      locale: 'ja_JP',
      lang: 'ja',
      siteName: '豆豆Pay - 日本での決済サービス',
      defaultTitle: '豆豆Pay - 日本での決済サービス',
      defaultDescription: '豆豆Payは日本での決済を安全かつ便利にするための総合的な決済サービスです。',
      keywords: '豆豆Pay, MamePay, 日本支付, 跨境支付, 移动支付, 电子支付, 決済サービス, クレジットカード, モバイル決済'
    },
    zh: {
      locale: 'zh_CN',
      lang: 'zh',
      siteName: '豆豆Pay - 日本支付服务',
      defaultTitle: '豆豆Pay - 日本支付服务',
      defaultDescription: '豆豆Pay提供全面的支付服务，让您在日本的支付安全又便捷。',
      keywords: '豆豆Pay, MamePay, 日本支付, 跨境支付, 移动支付, 电子支付, 支付服务, 信用卡, 移动支付'
    }
  };

  return configs[language as keyof typeof configs] || configs.ja;
};

export const useSEOConfig = (config?: Partial<SEOConfig>) => {
  const { i18n } = useTranslation();
  const baseURL = getBaseURL();
  const langConfig = getLanguageConfig(i18n.language);

  const fullConfig: SEOConfig = {
    title: config?.title || langConfig.defaultTitle,
    description: config?.description || langConfig.defaultDescription,
    keywords: config?.keywords || langConfig.keywords,
    image: config?.image || `${baseURL}/memapay-logo.png`,
    url: config?.url || baseURL,
    type: config?.type || 'website',
    locale: config?.locale || langConfig.locale,
    siteName: config?.siteName || langConfig.siteName
  };

  return {
    ...fullConfig,
    langConfig,
    baseURL,
    currentLanguage: i18n.language
  };
};

export const generateHreflangTags = (currentPath: string) => {
  const baseURL = getBaseURL();
  const hreflangTags = [];

  // 暂时只添加默认语言版本，避免指向不存在的路径
  // TODO: 当添加多语言路由支持后再启用多语言hreflang标签
  hreflangTags.push({
    rel: 'alternate',
    hreflang: 'x-default',
    href: `${baseURL}${currentPath}`
  });

  return hreflangTags;
};

export const generateStructuredData = (config: SEOConfig, type: 'organization' | 'website' = 'website') => {
  const baseURL = getBaseURL();

  if (type === 'organization') {
    return {
      '@context': 'https://schema.org',
      '@type': 'Organization',
      name: config.siteName,
      description: config.description,
      url: baseURL,
      logo: `${baseURL}/memapay-logo.png`,
      sameAs: [
        // Add social media URLs here if available
      ],
      contactPoint: {
        '@type': 'ContactPoint',
        telephone: '+81-3-1234-5678',
        contactType: 'customer service',
        availableLanguage: ['Japanese', 'Chinese']
      },
      address: {
        '@type': 'PostalAddress',
        streetAddress: '東京都港区六本木1-1-1',
        addressLocality: 'Tokyo',
        addressCountry: 'JP'
      }
    };
  }

  return {
    '@context': 'https://schema.org',
    '@type': 'WebSite',
    name: config.siteName,
    description: config.description,
    url: baseURL,
    inLanguage: ['ja', 'zh'],
    potentialAction: {
      '@type': 'SearchAction',
      target: {
        '@type': 'EntryPoint',
        urlTemplate: `${baseURL}/search?q={search_term_string}`
      },
      'query-input': 'required name=search_term_string'
    }
  };
};
